<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Users extends Model
{
    protected $table = 'users';
    protected $primaryKey = 'id';

    public static function getAdminByUsername($username)
    {
        return self::where("username",$username)->first();
    }

    public static function getUserByUserName($username)
    {
        return self::where("username",$username)->first();
    }

    public static function adminLists()
    {
        return self::where("platfrom",1)->get();
    }

    public static function updateStatus($id,$status)
    {
        return self::where("id",$id)->update(["status"=>$status]);
    }

    public static function creatHutongUsers($username,$openid,$code,$keyword){
        return self::insertGetId(["username"=>$username,"openid"=>$openid,"code"=>$code,"keyword"=>$keyword,"created_at"=>time()]);
    }

    public static function linksUsersList($keyword,$nike)
    {
        $query = self::select('users.*','task.endreason')->leftjoin('task', 'users.username', '=', 'task.phone');
        if (!empty($nike) && !is_numeric($nike)) $query->where('users.'.$keyword,"like","%".$nike."%");
        if (!empty($nike) && is_numeric($nike)) $query->where('users.'.$keyword,$nike);
        return $query->orderBy("id","DESC")->paginate(25);
    }

    public static function updateDsiable($id,$disable)
    {
    	return self::where("id",$id)->update(["disable"=>$disable]);
    }

    public static function getUsersByOpenid($openid)
    {
        return self::select("id","username")->where("openid",$openid)->first();
    }

    public static function getUsersByUnionid($unionid)
    {
        return self::select("id","username","nike","avatar","disable","gender","money","address","member_product_id","member_end_time","wechat_openid","created_at")->where("unionid",$unionid)->first();
    }

    public static function getUsersByWebOpenid($wechat_openid)
    {
        return self::select("id","username","nike","avatar","disable","gender","money","address","member_product_id","member_end_time")->where("wechat_openid",$wechat_openid)->first();
    }

    public static function creatByOpenId($openid,$avatar,$nike)
    {
        return self::insertGetId(["openid"=>$openid,"avatar"=>$avatar,"nike"=>$nike,"created_at"=>time()]);
    }

    public static function updateByOpenId($openid,$avatar,$nike)
    {
        return self::where("openid",$openid)->update(["avatar"=>$avatar,"nike"=>$nike]);
    }

    public static function creatUsersByWebOpenid($wechat_openid,$avatar,$nike,$gender,$address,$unionid)
    {
        return self::insertGetId(["wechat_openid"=>$wechat_openid,"avatar"=>$avatar,"nike"=>$nike,"gender"=>$gender,"address"=>$address,"unionid"=>$unionid,"platfrom"=>2,"created_at"=>time()]);
    }

    public static function creatUsersByAll($wechat_openid,$avatar,$nike,$gender,$address,$unionid,$username)
    {
        return self::insertGetId(["wechat_openid"=>$wechat_openid,"avatar"=>$avatar,"nike"=>$nike,"gender"=>$gender,"address"=>$address,"unionid"=>$unionid,"username"=>$username,"platfrom"=>2,"created_at"=>time()]);
    }

    public static function creatUsersByOfficial($wechat_openid,$gender,$address,$unionid,$token,$created_at)
    {
        return self::insertGetId(["wechat_openid"=>$wechat_openid,"gender"=>$gender,"address"=>$address,"unionid"=>$unionid,"platfrom"=>2,"token"=>$token,"created_at"=>$created_at]);
    }

    public static function updateUsersByOfficial($wechat_openid,$gender,$address,$unionid,$token)
    {
        return self::where("unionid",$unionid)->update(["wechat_openid"=>$wechat_openid,"gender"=>$gender,"address"=>$address,"token"=>$token]);
    }

    public static function updateUserOpenId($id,$wechat_openid,$unionid)
    {
        return self::where("id",$id)->update(["wechat_openid"=>$wechat_openid,"unionid"=>$unionid]);
    }

    public static function miniapBindWebOpenid($users_id,$openid,$avatar,$nike,$gender,$address,$unionid)
    {
        return self::where("id",$users_id)->update(["openid"=>$openid,"avatar"=>$avatar,"nike"=>$nike,"gender"=>$gender,"address"=>$address,"unionid"=>$unionid]);
    }

    public static function bindWebOpenid($users_id,$wechat_openid,$avatar,$nike,$gender,$address,$unionid)
    {
        return self::where("id",$users_id)->update(["wechat_openid"=>$wechat_openid,"avatar"=>$avatar,"nike"=>$nike,"gender"=>$gender,"address"=>$address,"unionid"=>$unionid]);
    }

    public static function updateUsersByWebOpenid($users_id,$wechat_openid,$avatar,$nike,$gender,$address,$unionid)
    {
        return self::where("id",$users_id)->update(["wechat_openid"=>$wechat_openid,"avatar"=>$avatar,"nike"=>$nike,"gender"=>$gender,"address"=>$address,"unionid"=>$unionid]);
    }


    public static function getUsersById($id)
    {
        return self::select("id","nike","avatar","disable","gender","money","address","service_name","service_code","name","openid","username","member_product_id","member_end_time")->where("id",$id)->first();
    }

    public static function getUsersWechatOpenid($id)
    {
        return self::select("username","wechat_openid")->where("id",$id)->first();
    }

    public static function getUsersWechatOpenidLists($ids)
    {
        return self::select("id","wechat_openid")->whereIn("id",$ids)->get();
    }

    public static function getUsersDetial($id)
    {
        return self::where("id",$id)->first();
    }

    public static function adminAdd($username,$password,$name,$authority)
    {
        return self::insertGetId(["username"=>$username,"password"=>$password,"name"=>$name,"nike"=>$name,"authority"=>$authority,"created_at"=>time()]);
    }

    public static function createUserByPhone($username)
    {
    	return self::insertGetId(["username"=>$username,"created_at"=>time()]);
    }

    public static function creatUsersByUnionid($unionid,$username,$code,$keyword,$device=1)
    {
        return self::insertGetId(["unionid"=>$unionid,"username"=>$username,"code"=>$code,"keyword"=>$keyword,'device'=>$device,"created_at"=>time()]);
    }

    public static function bindUnionidByUserName($unionid,$username)
    {
        return self::where("username",$username)->update(["unionid"=>$unionid]);
    }

    public static function bindScanUnionidByUserName($username,$unionid,$wechat_openid)
    {
        return self::where("username",$username)->update(["unionid"=>$unionid,"wechat_openid"=>$wechat_openid]);
    }

    public static function phoneRegister($username,$password)
    {
        return self::insertGetId(["username"=>$username,"password"=>$password,"created_at"=>time()]);
    }

    public static function phoneBind($id,$username,$password)
    {
        return self::where("id",$id)->update(["username"=>$username,"password"=>$password]);
    }

    public static function incrementUserScore($users_id, $score)
    {
        return self::where('id', $users_id)->increment("score", $score);
    }

    public static function decrementUserScore($users_id, $score)
    {
        return self::where('id', $users_id)->decrement("score", $score);
    }

    public static function updateUsersPassword($id,$password)
    {
        return self::where("id",$id)->update(["password"=>$password]);
    }

    public static function updateInvitationQrcode($id,$invitation_qrcode)
    {
        return self::where("id",$id)->update(["invitation_qrcode"=>$invitation_qrcode]);
    }

    public static function updateUsersInfo($id,$nike,$authority,$money)
    {
        return self::where("id",$id)->update(["nike"=>$nike,"authority"=>$authority,"money"=>$money]);
    }

    public static function apiUpdateUsersPassword($id,$username,$password)
    {
        return self::where("id",$id)->update(["username"=>$username,"password"=>$password]);
    }

    public static function updateUsersName($id,$username)
    {
        return self::where("id",$id)->update(["username"=>$username]);
    }

     public static function updateKeyword($id,$code,$keyword,$device=1)
    {
        return self::where("id",$id)->update(["keyword"=>$keyword,"code"=>$code,'device'=>$device]);
    }

    public static function getAdminByToken($token)
    {
        return self::select("id","username","wechat_openid","unionid","created_at")->where("token",$token)->first();
    }

    public static function getAdminByTokens($id)
    {
        return self::select("id","username","created_at")->where("id",$id)->first();
    }

    public static function emptyUsersToken($id)
    {
        return self::where("id",$id)->update(["token"=>""]);
    }

    public static function getUsersScode($id)
    {
        return self::select("score")->where("id",$id)->first();
    }

    public static function decrementUsersScode($id,$score)
    {
        return self::where('id',$id)->decrement("score",$score);
    }

    public static function deleteUser($id)
    {
        return self::where('id',$id)->delete();
    }

    public static function updateUsersMemberTime($id,$member_product_id,$member_end_time)
    {
        return self::where("id",$id)->update(["member_product_id"=>$member_product_id,"member_end_time"=>$member_end_time]);
    }

    public static function getUsersByOpenUserid($open_userid)
    {
        return self::where("open_userid",$open_userid)->first();
    }

    public static function creatUsersByWork($open_userid,$nike,$code,$avatar)
    {
        return self::insertGetId(["open_userid"=>$open_userid,"nike"=>$nike,"avatar"=>$avatar,"code"=>$code,"created_at"=>time()]);
    }
}
